//
//  UILabel+CL.m
//  IntegralMall
//
//  Created by 朱跃龙 on 2017/9/26.
//  Copyright © 2017年 朱跃龙. All rights reserved.
//

#import "UILabel+CL.h"

@implementation UILabel (CL)

+(UILabel *)addLabelWithFrame:(CGRect)frame AndText:(NSString *)text AndFont:(CGFloat)font AndAlpha:(CGFloat)alpha AndColor:(UIColor *)color
{
    UILabel *label =[[UILabel alloc]initWithFrame:frame];
    label.text = text;
    label.alpha = alpha;
    
    //    label.font =[UIFont systemFontOfSize:font];
    
    label.font =[UIFont fontWithName:@"HiraKakuProN-W3" size:font * H];
    label.textColor = color;
    label.textAlignment =NSTextAlignmentLeft;
    //    label.textAlignment =NSTextAlignmentCenter;
    return label;
}

+(UILabel *)addLabelWithFrame:(CGRect)frame Font:(CGFloat)font{
    
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    
    label.font =[UIFont fontWithName:@"HiraKakuProN-W3" size:font * H];
    
    return label;
    
}

+ (UILabel *)addLableFont:(CGFloat)font {
    
    UILabel *label = [[UILabel alloc] init];
    
    //    label.font =[UIFont fontWithName:@"HiraKakuProN-W3" size:font];
    
    label.font = [UIFont systemFontOfSize:font];
    
    return label;
    
}

+ (UILabel *)addLableFont:(CGFloat)font WithColor:(UIColor *)color{
    
    UILabel *label = [[UILabel alloc] init];
    
    label.textColor = color;
    
    //    label.font =[UIFont fontWithName:@"HiraKakuProN-W3" size:font];
    
    label.font = [UIFont systemFontOfSize:font];
    
    return label;
    
}

- (void)alignTop
{
    // 对应字号的字体一行显示所占宽高
    CGSize fontSize = [self.text sizeWithAttributes:@{NSFontAttributeName:self.font}];
    // 多行所占 height*line
    double height = fontSize.height*self.numberOfLines;
    // 显示范围实际宽度
    double width = self.frame.size.width;
    // 对应字号的内容实际所占范围
    CGSize stringSize = [self.text boundingRectWithSize:CGSizeMake(width, height) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:self.font} context:nil].size;
    // 剩余空行
    NSInteger line = (height - stringSize.height) / fontSize.height;
    // 用回车补齐
    for (int i = 0; i < line; i++) {
        
        self.text = [self.text stringByAppendingString:@"\n "];
    }
}
- (void)alignBottom
{
    CGSize fontSize = [self.text sizeWithAttributes:@{NSFontAttributeName:self.font}];
    double height = fontSize.height*self.numberOfLines;
    double width = self.frame.size.width;
    CGSize stringSize = [self.text boundingRectWithSize:CGSizeMake(width, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.font} context:nil].size;
    
    NSInteger line = (height - stringSize.height) / fontSize.height;
    // 前面补齐换行符
    for (int i = 0; i < line; i++) {
        self.text = [NSString stringWithFormat:@" \n%@", self.text];
    }
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    return (action == @selector(copyText:));
}

- (void)attachTapHandler {
    self.userInteractionEnabled = YES;
    UILongPressGestureRecognizer *g = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [self addGestureRecognizer:g];
}

//  处理手势相应事件
- (void)handleTap:(UIGestureRecognizer *)g {
    [self becomeFirstResponder];
    
    UIMenuItem *item = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copyText:)];
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObject:item]];
    [[UIMenuController sharedMenuController] setTargetRect:self.frame inView:self.superview];
    [[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
    
}

//  复制时执行的方法
- (void)copyText:(id)sender {
    //  通用的粘贴板
    UIPasteboard *pBoard = [UIPasteboard generalPasteboard];
    
    //  有些时候只想取UILabel的text中的一部分
    if (objc_getAssociatedObject(self, @"expectedText")) {
        pBoard.string = objc_getAssociatedObject(self, @"expectedText");
    } else {
        
        //  因为有时候 label 中设置的是attributedText
        //  而 UIPasteboard 的string只能接受 NSString 类型
        //  所以要做相应的判断
        if (self.text) {
            pBoard.string = self.text;
        } else {
            pBoard.string = self.attributedText.string;
        }
    }
}

- (BOOL)canBecomeFirstResponder {
    return [objc_getAssociatedObject(self, @selector(isCopyable)) boolValue];
}

- (void)setIsCopyable:(BOOL)number {
    objc_setAssociatedObject(self, @selector(isCopyable), [NSNumber numberWithBool:number], OBJC_ASSOCIATION_ASSIGN);
    [self attachTapHandler];
}

- (BOOL)isCopyable {
    return [objc_getAssociatedObject(self, @selector(isCopyable)) boolValue];
}


@end
